package com.hu.ta.assignment5.beans;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.hu.ta.assignment5.evaluation.ComparisonResults;

public class AnnotatedText {
	private List<AnnotatedToken> tokens;
	private Iterator<AnnotatedToken> tokIt;

	public AnnotatedText() {
		tokens = new ArrayList<AnnotatedToken>();
		tokIt = null;
	}

	public void addLine(String line) {
		for (String str : line.split(" ")) {
			String strs[] = str.split("/");
			StringBuffer buffer = new StringBuffer();
			for (int i = 0; i < strs.length - 1; i++) {
				buffer.append(strs[i]);
				buffer.append("/");
			}
			String bufout = buffer.toString();

			tokens.add(new AnnotatedToken(bufout.substring(0,
					bufout.length() - 1), strs[strs.length - 1]));
		}
	}

	public void addTaggedToken(AnnotatedToken annToken) {
		tokens.add(annToken);
	}

	public void addTaggedToken(String tok, String pos) {
		addTaggedToken(new AnnotatedToken(tok, pos));
	}

	public int size() {
		return tokens.size();
	}

	public String getLine(int index) {
		return tokens.get(index).toString();
	}

	public List<AnnotatedToken> getTokens() {
		return tokens;
	}

	public void addSentence(List<AnnotatedToken> sentence) {
		tokens.addAll(sentence);
	}

	public List<String> getNextSentence() {
		if (tokIt == null) {
			tokIt = tokens.iterator();
		}

		List<String> sentence = new ArrayList<String>();
		while (tokIt.hasNext()) {
			String tok = tokIt.next().getToken();
			sentence.add(tok);
			if (tok.equals(".")) {
				return sentence;
			}
		}

		return sentence;
	}

	/**
	 * @param tagSentence
	 */
	public void addText(AnnotatedText tagSentence) {
		tokens.addAll(tagSentence.tokens);
	}

	public ComparisonResults compareTo(AnnotatedText text2) {
		int wrong = 0;
		int num = 0;

		Iterator<AnnotatedToken> it1 = this.tokens.iterator();
		Iterator<AnnotatedToken> it2 = text2.tokens.iterator();

		while (it1.hasNext() && it2.hasNext()) {
			if (!it1.next().equals(it2.next())) {
				wrong++;
			}
			num++;
		}
		return new ComparisonResults(num, wrong);
	}
}
